package com.shell.customer.base.service.impl;

import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.persistence.annotation.Pagination;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.bee.persistence.service.impl.PersistentServiceImpl;
import com.shell.customer.base.dao.CusTagDao;
import com.shell.customer.base.service.CusTagBizService;
import com.shell.customer.base.service.CusTagRecService;
import com.shell.customer.base.service.CusTagService;
import com.shell.customer.po.CusTag;
import com.shell.customer.po.CusTagBiz;
import com.shell.customer.po.CusTagRec;
import com.shell.customer.tag.vo.CusTagVo;
import com.shell.feign.PubBusinessFeign;
import com.shell.governor.modules.po.PubBusiness;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service("cusTagService")
public class CusTagServiceImpl extends PersistentServiceImpl<CusTag> implements CusTagService {
	@Autowired
	private CusTagBizService cusTagBusRelService;
	@Autowired
	private PubBusinessFeign pubBusinessFeign;
	@Autowired
	private CusTagRecService cusTagRecService;
	@Autowired
	private CusTagDao cusTagDao;

	@Pagination
	public List<CusTag> selectTagsByPage(PageLimit limit, CusTag tag) {
		Example example = new Example(CusTag.class);
		Example.Criteria criteria = example.createCriteria();
		if (!StringUtil.emptyAndNull(tag.getTagName())) {
			tag.setTagName(StringUtil.getParameterLike(tag.getTagName()));
			criteria.andLike("tagName", tag.getTagName());
		}
		List<CusTag> tags = selectByExample(example);
		Map<String, String> idNames = getAllBusIdNameMap();
		for (CusTag ctag : tags) {
			List<CusTagBiz> tagBusRels = this.cusTagBusRelService.selectBusRelByTagId(ctag.getTagId());
			List<String> busNames = new ArrayList();
			for (CusTagBiz rel : tagBusRels) {
				String busName = (String) idNames.get(rel.getBizId());
				if (!StringUtil.emptyAndNull(busName)) {
					busNames.add(busName);
				}
			}
			ctag.setBusNames(busNames);
		}
		return tags;
	}

	private Map<String, String> getAllBusIdNameMap() {
		List<PubBusiness> list = this.pubBusinessFeign.getAllBus();
		Map<String, String> result = new HashMap();
		for (PubBusiness bus : list) {
			result.put(bus.getBizId(), bus.getBizName());
		}
		return result;
	}

	public boolean checkCanDeleteTag(String tagId) {
		List<CusTagRec> tagRecs = this.cusTagRecService.selectCusTagRecByTagId(tagId);
		return (tagRecs != null) && (tagRecs.size() > 0);
	}

	public CusTag selectCusTagByCode(String tagCode) {
		CusTag cusTag = new CusTag();
		cusTag.setTagCode(tagCode);
		return (CusTag) selectOne(cusTag);
	}

	public List<PubBusiness> selectBusList() {
		return this.pubBusinessFeign.getAllBus();
	}

	public boolean checkTagName(String tagId, String tagName) {
		Example example = new Example(CusTag.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("tagName", tagName);
		if (StringUtil.notTrimEmpty(tagId)) {
			criteria.andNotEqualTo("tagId", tagId);
		}
		List<CusTag> cusList = selectByExample(example);
		if ((cusList != null) && (cusList.size() > 0)) {
			return true;
		}
		return false;
	}

	public boolean checkTagCode(String tagId, String tagCode) {
		Example example = new Example(CusTag.class);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo("tagCode", tagCode);
		if (StringUtil.notTrimEmpty(tagId)) {
			criteria.andNotEqualTo("tagId", tagId);
		}
		List<CusTag> cusList = selectByExample(example);
		if ((cusList != null) && (cusList.size() > 0)) {
			return true;
		}
		return false;
	}

	public int deleteCusTag(String tagId) {
		this.cusTagBusRelService.deleteBusRelByTagId(tagId);
		return delete(tagId);
	}

	public CusTagVo selectCusTagAndBusRel(String tagId) {
		CusTag tag = (CusTag) selectByKey(tagId);
		CusTagVo tagVo = new CusTagVo();
		BeanUtils.copyProperties(tag, tagVo);
		CusTagBiz rel = new CusTagBiz();
		rel.setTagId(tagId);
		List<CusTagBiz> rels = this.cusTagBusRelService.selectByEntity(rel);
		List<String> ids = new ArrayList();
		for (CusTagBiz busRel : rels) {
			ids.add(busRel.getBizId());
		}
		tagVo.setBusIds(ids);
		return tagVo;
	}

	public int saveOrUpdateCusTag(CusTag tag, String busIds, String bizCodes) {
		boolean isAdd = true;
		if (StringUtil.emptyAndNull(tag.getTagId())) {
			tag.setTagId(SystemUtil.getUUID());
		} else {
			isAdd = false;
		}
		String[] ids = busIds.split(",");
		String[] codes = bizCodes.split(",");
		this.cusTagBusRelService.deleteBusRelByTagId(tag.getTagId());
		int index = 0;
		for (String busId : ids) {
			CusTagBiz rel = new CusTagBiz();
			rel.setBizId(busId);
			rel.setTagId(tag.getTagId());
			rel.setBizCode(codes[index]);
			this.cusTagBusRelService.save(rel);
			index++;
		}
		int count = saveOrUpdate(tag);
		if (!isAdd) {
			this.cusTagRecService.syncCusBizByTagId(tag.getTagId(), busIds);
		}
		return count;
	}

	public List<CusTag> getCusTagForEnt(String entId) {
		return getCusTagForEnt(entId, null);
	}

	public List<CusTag> getCusTagForEnt(String entId, String grpId) {
		return this.cusTagDao.getCusTagForEnt(entId, grpId);
	}

	public boolean hasTag(String cusId, String grpId, String tagCode) {
		boolean flag = false;
		List<CusTag> tags = getCusTagForEnt(cusId, grpId);
		for (CusTag tag : tags) {
			if (tagCode.equals(tag.getTagCode())) {
				flag = true;
				break;
			}
		}
		return flag;
	}

	public List<String> selectGrpsById(String cusId) {
		return this.cusTagDao.selectGrpsById(cusId);
	}

	public List<CusTag> selectCusTagByGrp(String grpId) {
		return this.cusTagDao.selectCusTagByGrp(grpId);
	}
}
